﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

// This class contains Lutefisk Specific Methods and Fields which aid in navigation, SQL interaction, and other functionality.
public class Lutefisk
{
        //Define super secret stuff
        public static readonly string APIKey = "816569bc7dc6ff9717d81dde78321615";
        public static readonly string APISecret = "52091cb1b4969b8f7f9e5b100764b297";    

        //The connection string for the SQL Database
        public static readonly string CONNECTION_STRING = "Data Source=mssql3.sqllive.co.uk;Integrated Security=False;"
                                               + "User ID=dataUser;Connect Timeout=15;Encrypt=False;Packet Size=4096;"
                                               + "password=password1";
        
        //Defines the sql injections to guard against
        public static readonly string SQL_CLEANSE_STRING = @"UPDATE|CREATE|DROP|[;,*]";       //takes care of sql injections
        
        //Defines the website    
        public static readonly string WEBSITE_URL = "http://facebookmusicians.smartsiteonline.net/";

        //Defines CV as a wacky enum type
        public enum CV
        {
            Invalid = 0, Valid = 1
        }

        // Generates a Secret ID based off our the Facebook UniqueID.
        public static long GetSecretLyeConcentration(long uID)
        {
            // Use a large wagstaff prime number
            //See New Mersenne Conjecture for more information and repunit prime

            long wagstaffPrime = 768614336404564651;        //Wagstaff = ((2^p +1)/3)
           
            long intermediateResult = wagstaffPrime / uID;
            
            // Multiply by a two sided prime
            long finalResult =  intermediateResult * 739397;

            return finalResult;
        }

        //An external page can verify that it should allow access to the user if the uID and lyeConcentration / "secret id" match.
        public static bool VerifySecretLyeConcentration(long uID, long lyeConcentration)
        {
            // Error checking
            if (lyeConcentration <= 0)
            {
                return false;
            }

            // Use a large wagstaff prime number
            long wagstaffPrime = 768614336404564651;

            long reverseIntermediateResult = lyeConcentration / 739397;

            long reverseduID = wagstaffPrime / reverseIntermediateResult;

            // Check if they match
            if (uID == reverseduID)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        //Directs a webbpage to outside facebook. Page.Response.Direct was not working so this is a Facebook Javascript (FBJS) workaround.
        public static void DirectExternal(Page page, string pageName)
        {
            // Construct a FBJS function.
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script>");
            sb.Append("    function navigate() {");
            sb.Append("        document.setLocation(\"" + pageName + "\");");
            sb.Append("        return false;");
            sb.Append("                 }");
            sb.Append("</script>");

            //Register the function definition. 
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScriptBlock", sb.ToString());
            }

            //Construct a FBJS block of code to call the function. 
            string funcCall = "<script> navigate(); </script>";

            // Register the calling code
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScript"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScript", funcCall);
            }
        }

    
        //Directs a webbpage in Facebook. Page.Response.Direct was not working so this is a Facebook Javascript (FBJS) workaround.
        public static void DirectPage(Page page, string pageName)
        {
            // Construct a FBJS function.
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script>");
            sb.Append("    function navigate() {");
            sb.Append("        document.setLocation(\"http://apps.facebook.com/lutefisk/" + pageName + ".aspx\");");
            sb.Append("        return false;");
            sb.Append("                 }");
            sb.Append("</script>");

            //Register the function definition. 
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScriptBlock", sb.ToString());
            }

            //Construct a FBJS block of code to call the function. 
            string funcCall = "<script> navigate(); </script>";

            // Register the calling code
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScript"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScript", funcCall);
            }
        }

        // Uses FBJS to display a Dialog box to the user.
        public static void ShowDialogBox(Page page, string Title, string Message)
        {
            // Construct a FBJS function.
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script>");
            sb.Append("    function showDialog() {");
            sb.Append("             new Dialog().showMessage(\"" + Title + "\", \"" + Message + "\");");
            sb.Append("        return false;");
            sb.Append("                 }");
            sb.Append("</script>");

            //Register the function definition.
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScriptBlock", sb.ToString());
            }

            //Construct a FBJS block of code to call the function.
            string funcCall = "<script> showDialog(); </script>";

            // Register the calling code
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScript"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScript", funcCall);
            }
        }
    
        //  Directs a webbpage in Facebook and shows a FBJS Messagebox. Page.Response.Direct was not working so this is a Facebook Javascript (FBJS) workaround. 
        public static void DirectPageAndShowDialog(Page page, string pageName,  string Title, string Message)
        {
            // Construct a FBJS function.
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<script>");
            sb.Append("function navigateAndShowDialog() {");
            sb.Append("var confirmDialog = new Dialog();");
            sb.Append("confirmDialog.showMessage(\"" + Title + "\", \"" + Message + "\");");
            sb.Append("confirmDialog.onconfirm = function() {");
            sb.Append("        document.setLocation(\"http://apps.facebook.com/lutefisk/" + pageName + ".aspx\");");
            sb.Append("     return true;");
            sb.Append(" }");
            sb.Append("}");
            sb.Append("</script>");

            //Register the function definition.
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScriptBlock", sb.ToString());
            }

            //Register the function definition.
            string funcCall = "<script> navigateAndShowDialog(); </script>";

            // Register the calling code
            if (!page.ClientScript.IsClientScriptBlockRegistered("JSScript"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), "JSScript", funcCall);
            }
        } 
}